import re
import xml.etree.ElementTree as ET


def to_lower_camel_case(s):
    # 将下划线分隔的字符串转换为小驼峰格式
    components = s.split('_')
    # 第一个单词首字母小写，其余单词首字母大写
    return components[0] + ''.join(x.title() for x in components[1:])


def extract_strings_from_xml(xml_file_path):
    tree = ET.parse(xml_file_path)
    root = tree.getroot()

    strings_dict = {}
    for string in root.findall('string'):
        name = string.get('name')
        value = string.text.strip() if string.text else ''
        if name and value:
            strings_dict[name] = value

    return strings_dict


def write_to_file(output_file_path, strings_dict):
    with open(output_file_path, 'a', encoding='utf-8') as file:
        for original_name, value in strings_dict.items():
            converted_name = to_lower_camel_case(original_name)
            file.write(f'LocaleKeys.{converted_name}: "{value}",\n')


def main():
    xml_file_path = './strings_zh.xml'
    output_file_path = './file2.dart'

    strings_dict = extract_strings_from_xml(xml_file_path)

    write_to_file(output_file_path, strings_dict)

    print(f"Data has been written to {output_file_path}")


if __name__ == "__main__":
    main()